package com.asset.dispoal.file.replyDetail.domain.service.command;

import com.alibaba.excel.EasyExcel;
import com.asset.dispoal.file.excel.MergeCellWriteHandler;
import com.asset.dispoal.file.replyDetail.domain.db.mapper.FileReplyDetailMapper;
import com.asset.dispoal.file.replyDetail.domain.dto.ReplyDetailExportTemplateId;
import com.asset.dispoal.file.replyDetail.dto.FileReplyExportRequest;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.exception.BusinessException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class FileReplyExportCommand {
    @Autowired
    private FileReplyDetailMapper fileReplyDetailMapper;

    public void execute(HttpServletResponse response, FileReplyExportRequest request) {
        List<ReplyDetailExportTemplateId> exportList = fileReplyDetailMapper.queryExportBySearch(request);
        // 导出报销人维度报销明细表
        try {
            List<CellRangeAddress> mergeRegions = new ArrayList<>();
            Map<String, Integer> idRowMap = new HashMap<>(); // id到起始行的映射
            int currentStartRow;

            for (int i = 0; i < exportList.size(); i++) {
                ReplyDetailExportTemplateId data = exportList.get(i);
                String id = data.getId();

                if (!idRowMap.containsKey(id)) {
                    // 如果是新的id，记录起始行
                    currentStartRow = i;
                    idRowMap.put(id, currentStartRow);
                } else {
                    // 如果是已有的id，检查是否需要合并
                    if (i == exportList.size() - 1 || !Objects.equals(exportList.get(i + 1).getId(), id)) {
                        // 如果是当前id的最后一行或下一行id不同，则创建合并区域
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 2, 2));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 3, 3));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 4, 4));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 5, 5));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 6, 6));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 7, 7));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 8, 8));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 9, 9));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 10, 10));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 11, 11));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 12, 12));
                        mergeRegions.add(new CellRangeAddress(idRowMap.get(id) + 1, i + 1, 13, 13));
                    }
                }
            }

            EasyExcel.write(response.getOutputStream())
                    .sheet("数据").
                    registerWriteHandler(new MergeCellWriteHandler(mergeRegions))
                    .head(ReplyDetailExportTemplateId.class).doWrite(exportList);

            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("导出数据.xlsx", "UTF-8"));
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");

        } catch (IOException e) {
            throw new BusinessException(e);
        }
    }
}
